//
//  HWAiPayWithdrawView.m
//  嗨文
//
//  Created by 响当当 on 2019/2/16.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWAiPayWithdrawView.h"

@interface HWAiPayWithdrawView()<UITextFieldDelegate>
@property (nonatomic, strong) UIView *footView;//
@property (nonatomic, strong) UITextField *nameTextField;//
@property (nonatomic, strong) UITextField *aiPayCountTextFiled;//
@property (nonatomic, strong) UITextField *aiPayCountSureTextFiled;//

@property (nonatomic, strong)NSString *nameStr;//姓名
@property (nonatomic, strong)NSString *aiPayCountStr;//支付宝账号
@property (nonatomic, strong)NSString *aiPayCountSureStr;//确定支付宝账号
@end

@implementation HWAiPayWithdrawView

- (UIView *)footView
{
    if (!_footView)
    {
        _footView = [[UIView alloc]initWithFrame:CGRectMake(63*kUIScaleBasePlus,187*kUIScaleBasePlus,250*kUIScaleBasePlus,44*kUIScaleBasePlus+197*kUIScaleBasePlus)];
        _footView.backgroundColor = [UIColor whiteColor];
        _footView.layer.masksToBounds = YES;
        _footView.layer.cornerRadius = 4;
        
        UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0,0,250*kUIScaleBasePlus,44*kUIScaleBasePlus)];
        headView.backgroundColor = LCHexColor(0xF54337);
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:headView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(4, 4)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = headView.bounds;
        maskLayer.path = maskPath.CGPath;
        headView.layer.mask  = maskLayer;
        [_footView addSubview:headView];
        
        
        NSString *apiName= IFNULL(LCUserDefaultsGet(HWAiPayName), @"");
        self.nameStr = apiName;
        
        UILabel *titleLabel = [HWUtilties creatLabelWithFrame:CGRectMake(0,16*kUIScaleBasePlus,250*kUIScaleBasePlus,13*kUIScaleBasePlus)
                                                 textAligment:NSTextAlignmentCenter
                                                     textClor:LCHexColor(0xFFFFFF)
                                                     textFont:14*kUIScaleBasePlus];
        titleLabel.backgroundColor = [UIColor clearColor];
        if ([apiName isValidString])
        {
            titleLabel.text = @"修改提现支付宝账号";
        }
        else
        {
            titleLabel.text = @"设置提现支付宝账号";
        }
        [_footView addSubview:titleLabel];
        
        UIButton *cancelBtn = [HWUtilties creatButtonWithFrame:CGRectMake(13*kUIScaleBasePlus,15*kUIScaleBasePlus,14*kUIScaleBasePlus,14*kUIScaleBasePlus)
                                                  textAligment:NSTextAlignmentLeft
                                                          text:@""
                                                      textClor:nil
                                                     buttonBGC:[UIColor clearColor]
                                                      textFont:0
                                                        target:self
                                                        method:@selector(remove)];
        [cancelBtn setImage:[UIImage imageNamed:@"quxiao"] forState:UIControlStateNormal];
        [_footView addSubview:cancelBtn];
        
        self.nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(45*kUIScaleBasePlus,CGRectGetMaxY(headView.frame)+29*kUIScaleBasePlus,166*kUIScaleBasePlus,13*kUIScaleBasePlus)];
        self.nameTextField.textAlignment = NSTextAlignmentCenter;
        self.nameTextField.placeholder = @"请输入您的身份证真实姓名";
        self.nameTextField.text = apiName;
        self.nameTextField.font = [UIFont systemFontOfSize:12*kUIScaleBasePlus];
        [self.nameTextField addTarget:self action:@selector(name:) forControlEvents:UIControlEventEditingChanged];
        self.nameTextField.delegate = self;
        self.nameTextField.tag = 0;
        [_footView addSubview:self.nameTextField];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(28*kUIScaleBasePlus,CGRectGetMaxY(self.nameTextField.frame)+12*kUIScaleBasePlus,195*kUIScaleBasePlus,1*kUIScaleBasePlus)];
        lineView.backgroundColor = LCHexColor(0xC6C6C6);
        [_footView addSubview:lineView];
        
        self.aiPayCountTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(45*kUIScaleBasePlus,CGRectGetMaxY(lineView.frame)+15*kUIScaleBasePlus,166*kUIScaleBasePlus,13*kUIScaleBasePlus)];
        self.aiPayCountTextFiled.textAlignment = NSTextAlignmentCenter;
        self.aiPayCountTextFiled.placeholder = @"请输入您的支付宝账号";
        NSString *apiCount= IFNULL(LCUserDefaultsGet(HWAiPayCount), @"");
        self.aiPayCountStr = apiCount;
        self.aiPayCountTextFiled.text = apiCount;
        self.aiPayCountTextFiled.font = [UIFont systemFontOfSize:12*kUIScaleBasePlus];
        [self.aiPayCountTextFiled addTarget:self action:@selector(aiPay:) forControlEvents:UIControlEventEditingChanged];
        self.aiPayCountTextFiled.delegate = self;
        self.aiPayCountTextFiled.tag = 1;
        [_footView addSubview:self.aiPayCountTextFiled];
        
        UIView *lineViewTwo = [[UIView alloc]initWithFrame:CGRectMake(28*kUIScaleBasePlus,CGRectGetMaxY(self.aiPayCountTextFiled.frame)+12*kUIScaleBasePlus,195*kUIScaleBasePlus,1*kUIScaleBasePlus)];
        lineViewTwo.backgroundColor = LCHexColor(0xC6C6C6);
        [_footView addSubview:lineViewTwo];
        
        self.aiPayCountSureTextFiled = [[UITextField alloc]initWithFrame:CGRectMake(45*kUIScaleBasePlus,CGRectGetMaxY(lineViewTwo.frame)+15*kUIScaleBasePlus,166*kUIScaleBasePlus,13*kUIScaleBasePlus)];
        self.aiPayCountSureTextFiled.textAlignment = NSTextAlignmentCenter;
        self.aiPayCountSureTextFiled.placeholder = @"请再次输入您的支付宝账号";
        NSString *apiSureCount= IFNULL(LCUserDefaultsGet(HWAiPayCount), @"");
        self.aiPayCountSureStr = apiSureCount;
        self.aiPayCountSureTextFiled.text = apiSureCount;
        self.aiPayCountSureTextFiled.font = [UIFont systemFontOfSize:12*kUIScaleBasePlus];
        [self.aiPayCountSureTextFiled addTarget:self action:@selector(aiPaySure:) forControlEvents:UIControlEventEditingChanged];
        self.aiPayCountSureTextFiled.delegate = self;
        self.aiPayCountSureTextFiled.tag = 2;
        [_footView addSubview:self.aiPayCountSureTextFiled];
        
        UIView *lineViewThree = [[UIView alloc]initWithFrame:CGRectMake(28*kUIScaleBasePlus,CGRectGetMaxY(self.aiPayCountSureTextFiled.frame)+12*kUIScaleBasePlus,195*kUIScaleBasePlus,1*kUIScaleBasePlus)];
        lineViewThree.backgroundColor = LCHexColor(0xC6C6C6);
        [_footView addSubview:lineViewThree];
        
        UIView *bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(lineViewThree.frame)+23*kUIScaleBasePlus,250*kUIScaleBasePlus,1*kUIScaleBasePlus)];
        bottomLineView.backgroundColor = LCHexColor(0xC6C6C6);
        [_footView addSubview:bottomLineView];
        
        UIButton *sureBtn = [HWUtilties creatButtonWithFrame:CGRectMake(0,CGRectGetMaxY(bottomLineView.frame),124.5*kUIScaleBasePlus,40*kUIScaleBasePlus)
                                                textAligment:NSTextAlignmentCenter
                                                        text:@"确定"
                                                    textClor:LCHexColor(0x000000)
                                                   buttonBGC:[UIColor clearColor]
                                                    textFont:14*kUIScaleBasePlus
                                                      target:self
                                                      method:@selector(sure)];
        UIBezierPath *maskPathSure = [UIBezierPath bezierPathWithRoundedRect:sureBtn.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(4, 4)];
        CAShapeLayer *maskLayerSure = [[CAShapeLayer alloc] init];
        maskLayerSure.frame = sureBtn.bounds;
        maskLayerSure.path = maskPathSure.CGPath;
        sureBtn.layer.mask  = maskLayerSure;
        [_footView addSubview:sureBtn];
        
        UIView *midleView = [[UIView alloc]initWithFrame:CGRectMake(124.5*kUIScaleBasePlus,CGRectGetMaxY(bottomLineView.frame),1*kUIScaleBasePlus,40*kUIScaleBasePlus)];
        midleView.backgroundColor = LCHexColor(0xC6C6C6);
        [_footView addSubview:midleView];
        
        UIButton *removeBtn = [HWUtilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(midleView.frame),CGRectGetMaxY(bottomLineView.frame),124.5*kUIScaleBasePlus,40*kUIScaleBasePlus)
                                                textAligment:NSTextAlignmentCenter
                                                        text:@"取消"
                                                    textClor:LCHexColor(0xFD4444)
                                                   buttonBGC:[UIColor clearColor]
                                                    textFont:14*kUIScaleBasePlus
                                                      target:self
                                                      method:@selector(remove)];
        UIBezierPath *maskPathCancel = [UIBezierPath bezierPathWithRoundedRect:removeBtn.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(4, 4)];
        CAShapeLayer *maskLayerCancel = [[CAShapeLayer alloc] init];
        maskLayerCancel.frame = removeBtn.bounds;
        maskLayerCancel.path = maskPathCancel.CGPath;
        sureBtn.layer.mask  = maskLayerCancel;
        [_footView addSubview:removeBtn];
        
    }
    return _footView;
}



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
        
        [self addSubview:self.footView];
    }
    return self;
}


#pragma mark - Method
//确定
- (void)sure
{
    if (![self.nameStr isValidString])
    {
        [SVProgressHUD showErrorWithStatus:@"姓名不能为空"];
        return;
    }
    
    if (![self.aiPayCountStr isValidString])
    {
        [SVProgressHUD showErrorWithStatus:@"支付宝账号不能为空"];
        return;
    }
    
    if (![self.aiPayCountSureStr isValidString] || ![self.aiPayCountSureStr isEqualToString:self.aiPayCountStr])
    {
        [SVProgressHUD showErrorWithStatus:@"确认支付宝输入错误，请重新输入"];
        return;
    }
    
    
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token,
                               @"aliPayName"   : self.nameStr,
                               @"aliPayAccount": self.aiPayCountStr
                               };
    
    WeakSelf(self)
    [MyInfoManager setAiPayCount:prarmers sucess:^(id  _Nonnull acceptData) {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showSuccessWithStatus:@"支付宝账号设置成功"];
        LCUserDefaultsSet(weakSelf.nameStr,HWAiPayName);
        LCUserDefaultsSet(weakSelf.aiPayCountStr,HWAiPayCount);
        [weakSelf remove];
    } fail:^(NSError * _Nullable error) {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showErrorWithStatus:@"设置支付宝账号失败，请检查网络是否良好"];
        DISMISS_SVP(1.0f);
    }];
    
}

//取消
- (void)remove
{
    [self removeFromSuperview];
}


//真实姓名
- (void)name:(UITextField *)textField
{
    self.nameStr = textField.text;
}

//输入支付宝
- (void)aiPay:(UITextField *)textField
{
    self.aiPayCountStr = textField.text;
}

//确认支付宝
- (void)aiPaySure:(UITextField *)textField
{
    self.aiPayCountSureStr = textField.text;
}

#pragma mark - textField delegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSInteger tag = textField.tag;
    
    WeakSelf(self)
    switch (tag)
    {
        case 0:
        {
            
        }
            break;
        case 1:
        {
            [UIView animateWithDuration:0.2 animations:^{
                [weakSelf.footView setY:167*kUIScaleBasePlus];
            }];
        }
            break;
        case 2:
        {
            [UIView animateWithDuration:0.2 animations:^{
                [weakSelf.footView setY:157*kUIScaleBasePlus];
            }];
        }
            break;
        default:
            break;
    }
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    WeakSelf(self)
    [UIView animateWithDuration:0.2 animations:^{
        [weakSelf.footView setY:187*kUIScaleBasePlus];
    }];
    return YES;
}

- (void)getDate:(HWCashWidthDrawModel *)model
{
    self.nameTextField.text = model.realName;
    self.nameStr = model.realName;
    
    self.aiPayCountTextFiled.text = model.alipayAccount;
    self.aiPayCountStr = model.alipayAccount;
    
    self.aiPayCountSureTextFiled.text = model.alipayAccount;
    self.aiPayCountSureStr = model.alipayAccount;
}
@end
